package com.j2eefast.produce.pms.controller;

import com.j2eefast.common.core.base.entity.Ztree;
import com.j2eefast.common.core.business.annotaion.BussinessLog;
import com.j2eefast.common.core.controller.BaseController;
import com.j2eefast.common.core.enums.BusinessType;
import com.j2eefast.common.core.utils.ResponseData;
import com.j2eefast.common.core.utils.ValidatorUtil;
import com.j2eefast.framework.annotation.RepeatSubmit;
import com.j2eefast.framework.sys.service.SysUserService;
import com.j2eefast.produce.pms.entity.PmsTestEntity;
import com.j2eefast.produce.pms.entity.PmsTestProjectClassifyEntity;
import com.j2eefast.produce.pms.service.PmsTestProjectClassifyService;
import com.j2eefast.produce.pms.service.PmsTestService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>检测项目分类</p>
 *
 * @author: summer
 * @date: 2021-05-29 22:03
 * @web: http://www.letukeji.cn
 * @version: 1.0.1
 */
@Controller
@RequestMapping("/pms/testProjectClassify")
public class PmsTestProjectClassifyController extends BaseController {
	private String urlPrefix = "modules/pms/testProjectClassify";
	@Autowired
	private PmsTestProjectClassifyService pmsTestProjectClassifyService;
	@Autowired
	private SysUserService sysUserService;

	@RequiresPermissions("pms:testProjectClassify:view")
	@GetMapping()
	public String process() {
		return urlPrefix + "/index";
	}

	/**
	 * 页面获取公司信息列表
	 * @param params
	 * @return
	 */
	@RequestMapping("/list")
	@RequiresPermissions("pms:testProjectClassify:list")
	@ResponseBody
	public List<PmsTestProjectClassifyEntity> list(@RequestParam Map<String, Object> params) {
		return  pmsTestProjectClassifyService.findList(params);
	}

	/**
	 * 加载公司列表树
	 */
	@GetMapping("/treeData")
	@RequiresPermissions("pms:testProjectClassify:list")
	@ResponseBody
	public List<Ztree> treeData(Long parentId){
		List<Ztree> ztrees = pmsTestProjectClassifyService.findCompTree(parentId);
		return ztrees;
	}

	/**
	 * 新增设备分类
	 */
	@GetMapping("/add/{id}")
	public String add(@PathVariable("id") Long id, ModelMap mmap){
		//mmap.put("comp",  busClassifyService.findCompById(id));
		PmsTestProjectClassifyEntity pmsTestProjectClassifyEntity = pmsTestProjectClassifyService.findCompById(id);
		mmap.put("classify",  pmsTestProjectClassifyEntity);
		return urlPrefix + "/add";
	}

//	/**
//	 * 新增参数配置
//	 */
//	@RequiresPermissions("pms:testProjectClassify:add")
//	@GetMapping("/add")
//	public String add() {
//		return urlPrefix + "/add";
//	}
	/**
	 * 保存配置
	 */
	@BussinessLog(title = "检验列表", businessType = BusinessType.INSERT)
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@RequiresPermissions("pms:testProjectClassify:add")
	@ResponseBody
	public ResponseData save(@Validated PmsTestProjectClassifyEntity comp) {
//		busClassifyService.checkDataScope(comp.getParentId());
		ValidatorUtil.validateEntity(comp);
//		if (!busClassifyService.checkCompNameUnique(comp)){
//			return error("设备分类'" + comp.getClassName() + "'失败,名称已存在");
//		}
		pmsTestProjectClassifyService.add(comp);
		return success();
	}

	/**
	 * 新增设备分类
	 */

	/**
	 * 新增参数配置
	 */
	@RequiresPermissions("pms:testProjectClassify:addfrom")
	@GetMapping("/addfrom")
	public String addfrom() {
		System.out.println("新增参数配置");
		return urlPrefix + "/addfrom";
	}

//	/**
//	 * 保存配置
//	 */
//	@BussinessLog(title = "检验列表", businessType = BusinessType.INSERT)
//	@RequestMapping(value = "/add", method = RequestMethod.POST)
//	@RequiresPermissions("bus:test:add")
//	@ResponseBody
//	public ResponseData save(@Validated PmsTestEntity comp) {
////		busClassifyService.checkDataScope(comp.getParentId());
//		ValidatorUtil.validateEntity(comp);
////		if (!busClassifyService.checkCompNameUnique(comp)){
////			return error("设备分类'" + comp.getClassName() + "'失败,名称已存在");
////		}
//		pmsTestService.add(comp);
//		return success();
//	}


	/**
	 * 修改
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap){
		PmsTestProjectClassifyEntity pmsTestProjectClassifyEntity = pmsTestProjectClassifyService.findCompById(id);

		mmap.put("projectClassify",  pmsTestProjectClassifyEntity);
		System.out.println("22323");
		System.out.println(pmsTestProjectClassifyEntity.getName());
		System.out.println("22323");
		//mmap.put("areaNames",  busClassifyService.getAreaNames(busClassifyEntity.getAreaIds()));
		return urlPrefix + "/edit";
	}

	/**
	 * 修改公司
	 */
	@BussinessLog(title = "检验列表", businessType = BusinessType.UPDATE)
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@RequiresPermissions("pms:testProjectClassify:edit")
	@ResponseBody
	public ResponseData update(@Validated PmsTestProjectClassifyEntity comp) {
		//check data scope
//		busClassifyService.checkDataScope(comp.getId());
		System.out.println("2222222");
		ValidatorUtil.validateEntity(comp);
//		if (!busClassifyService.checkCompNameUnique(comp)) {
//			return error("修改公司'" + comp.getName() + "'失败,名称已存在");
//		}
		System.out.println("2222222");

		System.out.println("22222222");
		if(pmsTestProjectClassifyService.update(comp)){
			return success();
		}else{
			return error("修改失败!");
		}
	}

	/**
	 * 删除检验
	 */
	/**
	 * 合同删除
	 */
	@RepeatSubmit
	@RequiresPermissions("pms:testProjectClassify:del")
	@BussinessLog(title = "删除", businessType = BusinessType.UPDATE)
	@RequestMapping(value = "/del", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData doDelete(HttpServletRequest request) {
		Long  ids=Long.parseLong(request.getParameter("id")) ;
		return pmsTestProjectClassifyService.delSysCompById(ids) ? success() : error("修改失败!");
	}

}
